﻿using System;
using System.IO;
using System.Text;
using System.Security.Cryptography;

namespace FrameWork.Core.Cryption
{
    /// <summary>
    /// DESC加解密
    /// </summary>
    public class DescCryption
    {
        private static TripleDESCryptoServiceProvider TripleDes = new TripleDESCryptoServiceProvider();
        /// <summary>
        /// 构造函数
        /// </summary>
        public DescCryption()
        {
        }
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="plaintext"></param>
        /// <returns></returns>
        public static string Encrypt(string plaintext)
        {
            string result;
            try
            {
                TripleDes.Key = TruncateHash("QYZN", TripleDes.KeySize / 8);
                TripleDes.IV = TruncateHash("", TripleDes.BlockSize / 8);
                byte[] bytes = Encoding.Unicode.GetBytes(plaintext);
                MemoryStream memoryStream = new MemoryStream();
                CryptoStream cryptoStream = new CryptoStream(memoryStream, TripleDes.CreateEncryptor(), CryptoStreamMode.Write);
                cryptoStream.Write(bytes, 0, bytes.Length);
                cryptoStream.FlushFinalBlock();
                result = Convert.ToBase64String(memoryStream.ToArray());
                memoryStream.Dispose();
                cryptoStream.Dispose();
            }
            catch (Exception)
            {
                result = "";
            }
            return result;
        }
        /// <summary>
        /// 解密
        /// </summary>
        /// <param name="encryptedtext"></param>
        /// <returns></returns>
        public static string Decrypt(string encryptedtext)
        {
            string result;
            try
            {
                TripleDes.Key = TruncateHash("QYZN", TripleDes.KeySize / 8);
                TripleDes.IV = TruncateHash("", TripleDes.BlockSize / 8);
                byte[] array = Convert.FromBase64String(encryptedtext);
                MemoryStream memoryStream = new MemoryStream();
                CryptoStream cryptoStream = new CryptoStream(memoryStream, TripleDes.CreateDecryptor(), CryptoStreamMode.Write);
                cryptoStream.Write(array, 0, array.Length);
                cryptoStream.FlushFinalBlock();
                result = Encoding.Unicode.GetString(memoryStream.ToArray());
                memoryStream.Dispose();
                cryptoStream.Dispose();
            }
            catch (Exception ex)
            {
                result = "";
            }
            return result;
        }
        /// <summary>
        /// 计算
        /// </summary>
        /// <param name="key"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        private static byte[] TruncateHash(string key, int length)
        {
            SHA1CryptoServiceProvider sHA1CryptoServiceProvider = new SHA1CryptoServiceProvider();
            byte[] bytes = Encoding.Unicode.GetBytes(key);
            byte[] array = sHA1CryptoServiceProvider.ComputeHash(bytes);
            byte[] array2 = new byte[length];
            if (array.Length >= array2.Length)
            {
                for (int i = 0; i < array2.Length; i++)
                {
                    array2[i] = array[i];
                }
            }
            else
            {
                for (int i = 0; i < array.Length; i++)
                {
                    array2[i] = array[i];
                }
            }
            return array2;
        }
    }
}